#include "attribmanager.h"
#include <sstream>


//-------------------------------------------------------------------------
AttribManager::AttribManager()
{
  // here we should read from files the default attributes prototypes
  // for testing, creates the attributes prototypes manually
  AttribPrototype* prototype1 = new AttribPrototype( "Tractions", "Arrow" );
  prototype1->applyOnEdge();
  prototype1->setProperty("Tx","REAL",1);
  prototype1->setProperty("Ty","REAL",2);
  AttribPrototype* prototype2 = new AttribPrototype( "Supports", "Triangle" );
  prototype2->applyOnEdge();
  prototype2->setProperty("Rx","BOOLEAN",1);
  prototype2->setProperty("Ry","BOOLEAN",2);
  AttribPrototype* prototype3 = new AttribPrototype( "Material", "M" );
  prototype3->applyOnFace();
  prototype3->setProperty("YoungsModulus","REAL",1);
  prototype3->setProperty("PoisonsRatio","REAL",1);
  prototype3->setProperty("Conductivity","REAL",1);
  prototype3->setProperty("Thickness","REAL",1);
  AttribPrototype* prototype4 = new AttribPrototype( "Force", "Arrow" );
  prototype4->applyOnVertex();
  prototype4->setProperty("Fx","REAL",1);
  prototype4->setProperty("Fy","REAL",2);
  AttribPrototype* prototype5 = new AttribPrototype( "Restrain", "Triangle" );
  prototype5->applyOnVertex();
  prototype5->setProperty("Rx","BOOLEAN",1);
  prototype5->setProperty("Ry","BOOLEAN",2);
  AttribPrototype* prototype6 = new AttribPrototype( "Temperature", "Circle" );
  prototype6->applyOnEdge();
  prototype6->setProperty("T","REAL",1);
  m_prototypes.push_back( prototype4 );
  m_prototypes.push_back( prototype5 );
  m_prototypes.push_back( prototype1 );
  m_prototypes.push_back( prototype2 );
  m_prototypes.push_back( prototype3 );
  m_prototypes.push_back( prototype6 );
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
int 
AttribManager::getNumberOfPrototypes()
{
  return m_prototypes.size();
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
string 
AttribManager::getPrototypeNameByIndex( int _index )
{
  return m_prototypes[_index]->getName();
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
string 
AttribManager::createAttributeFromPrototype( int _index )
{
  // get the name of the "index" prototype to create the name of the new attribute
  string name = m_prototypes[_index]->getName();
  int nattrib = m_attributes[name].size();
  // convert INT to STRING
  std::ostringstream sstream;
  sstream << (nattrib+1);
  string snattrib = sstream.str();
  string attribName = name+"_"+snattrib;

  // get the "index" prototype to create a new attribute
  Attribute* attrib =  new Attribute( attribName, m_prototypes[_index] );
  m_attributes[name].push_back( attrib );



  return attrib->getName();
}
//-------------------------------------------------------------------------



//-------------------------------------------------------------------------
Attribute* 
AttribManager::getAttributeByName( string _name )
{
  map<string,vector<Attribute*> >::iterator it;
  for( it=m_attributes.begin(); it!=m_attributes.end(); it++ )
  {
    vector<Attribute*> vAttrib = it->second;
    for( int i=0; i<vAttrib.size(); i++ )
    {
      if( vAttrib[i]->getName() == _name )
      {
        return vAttrib[i];
      }
    }
  }
  return NULL;
}
//-------------------------------------------------------------------------
